<?php
/**
 * 处理警员的添加、修改、删除，根据传递过来的act判断相应的操作
 */
session_start();
if (!isset ($_SESSION["id"])) {
	session_destroy();
	echo "<script language='javascript'>top.location.href = 'Login.php';</script>";
	exit;
}


if(isset($_POST['act'])||isset($_GET['act'])){
	include_once 'public/DB_Oracle.php';
	include_once 'public/Json.php';
	$DBsql = new DB_Oracle ();
	$act=(!empty($_POST['act']))?$_POST['act']:$_GET['act'];
	

$columns=array(
	array('ID','REF_ID'),
	array('车牌号','CAR_ID'),
	array('油品种类','OIL_TYPE'),
	array('加油时间','REF_TIME','r'),
	array('金额','REF_SUM','n'),
	array('单价','UNIT_PRICE','n'),
	array('油量','REF_OIL','n'),
	array('出车单号','OUT_CAR_NUMB')
		);
;
$rcol=array(0,1);
$title="加油表";

//序号对应列
$add_array=array(
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
);
$edit_array=array(
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
);


$query=0;
if(isset($_GET['query'])){
$query=$_GET['query'];
}
if(isset($_POST['query'])){
$query=$_POST['query'];
}
$table='GPS_COST_REFUEL';

switch($query)
{
	case 0;
	$table='GPS_COST_REFUEL';
	//$columns=array('ID','车牌号','油品种类','加油时间','金额','单价','油量','出车单号');
	break;	
	case 1;
	$table='GPS_COST_REFUEL';
	//$columns=array('ID','车牌号','油品种类','加油时间','金额','单价','油量','出车单号');
	break;
	case 2;
	$table='GPS_COST_REFUEL';
	break;
	case 3;
	$table='GPS_COST_EXPENSE';
	$columns=array(
	array('ID','ID'),
	array('车牌号','CAR_ID'),
	array('时间','XF_TIME','r'),
	array('杂支类型','ZC_TYPE'),
	array('金额','MONEY','n'),
	array('出车单','OUT_CAR_NUMB'),
	array('备注说明','REMARK')
		);
	$title='杂支表';
	break;
	case 4;
	$table='GPS_COST_MAINTENANCE';
	$columns=array(
	array('ID','ID'),
	array('车牌号','CAR_ID'),
	array('时间','BY_TIME','r'),
	array('行驶里程','MILEAGE','n'),
	array('金额','SUM','n'),
	array('联系人','CONTACT'),
	array('联系电话','PHONE'),
	array('维修地点','ADDRESS'),
	array('出车单','OUT_CAR_NUMB'),
	array('备注','REMARK')
		);
	$title='保养表';
	break;
	case 5;
	$table='GPS_COST_SERVICE';
	$columns=array(
	array('ID','ID'),
	array('车牌号','CAR_ID'),
	array('时间','WX_TIME','r'),
	array('个人出钱','GR_MONEY','n'),
	array('保险出钱','BX_MONEY','n'),
	array('公司出钱','GS_MONEY','n'),
	array('联系人','CONTACT'),
	array('联系电话','PHONE'),
	array('维修地点','ADDRESS'),
	array('出车单','OUT_CAR_NUMB'),
	array('备注','REMARK')
	);
	$title='维修表';
	break;
	case 6;
	$table='GPS_COST_SAFETY';
	$columns=array(
	array('ID','ID'),
	array('车牌号','CAR_ID'),
	array('开始时间','START_TIME','r'),
	array('结束时间','END_TIME','r'),
	array('保险公司','BX_NAME'),
	array('保险种类','BX_TYPE'),
	array('金额','MONEY','n'),
	array('备注','REMARK')
	);
	$title='保险表';
	break;
	case 7;
	$table='GPS_COST_OVERHAUL';
	$columns=array(
	array('ID','ID'),
	array('车牌号','CAR_ID'),
	array('开始时间','START_TIME','r'),
	array('结束时间','END_TIME','r'),
	array('代办人','DB_CONTACT'),
	array('联系电话','PHONE'),
	array('金额','MONEY','n'),
	array('备注','REMARK')
	);
	$title='年检表';
	break;
	default:
	$table='GPS_COST_REFUEL';
	break;
}
	
	if ($act=='chuli') {//查询某警员相关信息，用于更新警员信息
		$sn=$_POST['sn'];
		$sn=rtrim($sn,',');
		$sql="update gps_car_use_approval g set g.SQ_STATUS=1 where g.id in ($sn)";
		//exit($sql);
		$row=$DBsql->query($sql);
		echo 'y';
	}else if ($act=='select') {//查询某警员相关信息，用于更新警员信息
		$sn=$_POST['sn'];
		$sql="SELECT *
  FROM ".$table."   
 WHERE  ".$columns[0][1]." = ".$sn."";

		$row=$DBsql->fetch_one_array($sql);
		//exit($sql);
		echo JSON($row);
	}else if($act=='up'){
		$usql='';
		   foreach ($add_array as $add_a)
		   { 
			   if($add_a<count($columns)){ 
			   	$usql.=$columns[$add_a][1];
				$usql.='=';
				if(count($columns[$add_a])>2 && $columns[$add_a][2]=='r')
				{
					$usql.="to_date('".$_POST[$columns[$add_a][1]]."', 'yyyy-mm-dd hh24:mi:ss')";
				}else if(count($columns[$add_a])>2 && $columns[$add_a][2]=='n')
				{
					$usql.=$_POST[$columns[$add_a][1]];
				}
				else $usql.=('\''.$_POST[$columns[$add_a][1]].'\'');
				$usql.=',';
			   }
		   }
		 $usql=rtrim($usql,',');
		 
		$sn=$_POST['sn'];

		$sql="update ".$table." set ".$usql." where ".$columns[0][1]."= '$sn'";
		

		
		if($DBsql->query($sql)){
			echo '{
				"info":"数据更新成功！",
				"status":"y"
			}';
		}else{
			echo '{
				"info":"数据更新失败！",
				"status":"n"
			}';
		}
	}else if($act=='valid'){//新增警员，先验证警号是否可用
		$number=$_POST['param'];//警号
		$sql="select dp.personnel_index,
			       dp.personnel_name,
			       dp.personnel_gender,
			       dp.personnel_sn,
			       dd.department_name,
       			   dd.department_index
			  from dep_personnel dp, dep_department dd
			 where dp.department_index = dd.department_index
			   and dp.personnel_sn='$number'
			 order by dp.personnel_index";
		$row=$DBsql->fetch_one_array($sql);
		if(isset($row['PERSONNEL_INDEX'])){
			echo '警号已经存在，请更换！';
		}else{
			echo 'y';
		}
	}else if($act=='add'){//增加新警员

		   $a=$columns[0][1];
		   $b=$table.'_SEQ.nextval';
		   foreach ($add_array as $add_a)
		   { 
			   if($add_a<count($columns)){ 
			   	$a.=',';
			   	$a.=$columns[$add_a][1];
				
				$b.=',';
				if(count($columns[$add_a])>2 && $columns[$add_a][2]=='r')
				{
					$b.="to_date('".$_POST[$columns[$add_a][1]]."', 'yyyy-mm-dd hh24:mi:ss')";
				}else if(count($columns[$add_a])>2 && $columns[$add_a][2]=='n')
				{
					$b.=$_POST[$columns[$add_a][1]];
				}
				else $b.=('\''.$_POST[$columns[$add_a][1]].'\'');
				
			   }
		   }
		   
		 $sql="insert into ".$table."(".$a.")values(".$b.")";
	 
	if($DBsql->query($sql)){
			echo '{
				"info":"数据更新成功！",
				"status":"y"
			}';
		}else{
			echo '{
				"info":"数据更新失败！",
				"status":"n"
			}';
		}
	}else if($act=='del'){
		if(substr($act,0,3)=='del'){
		$sn=$_POST['sn'];
		$sql="delete from ".$table." dp where dp.".$columns[0][1]." in ($sn)";
		$DBsql->query($sql);
		echo 'y';
		}
	}
}
?>